package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.cderp.domain.Material;
import com.ruoyi.cderp.domain.TblOrderPayType;
import com.ruoyi.cderp.mapper.BaseDataProductMapper;
import com.ruoyi.cderp.mapper.MaterialMapper;
import com.ruoyi.cderp.mapper.TblOrderPayTypeMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.TblPurchaseOrderDetail;
import com.ruoyi.cderp.mapper.TblPurchaseOrderMapper;
import com.ruoyi.cderp.domain.TblPurchaseOrder;
import com.ruoyi.cderp.service.ITblPurchaseOrderService;

/**
 * 采购订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-18
 */
@Service
public class TblPurchaseOrderServiceImpl implements ITblPurchaseOrderService
{
    // 存储当前登录用户对象
    LoginUser loginUser = null;
    @Autowired
    private TblPurchaseOrderMapper tblPurchaseOrderMapper;
    @Autowired
    private BaseDataProductMapper baseDataProductMapper;
    @Autowired
    private TblOrderPayTypeMapper tblOrderPayTypeMapper;
    @Autowired
    private MaterialMapper materialMapper;

    /**
     * 查询采购订单
     *
     * @param id 采购订单主键
     * @return 采购订单
     */
    @Override
    public TblPurchaseOrder selectTblPurchaseOrderById(String id)
    {
        TblPurchaseOrder tblPurchaseOrder=tblPurchaseOrderMapper.selectTblPurchaseOrderById(id);
        for (TblPurchaseOrderDetail detail:tblPurchaseOrder.getTblPurchaseOrderDetailList()){
            int num=detail.getPurchaseType();
            if (num==1){
                Material material=materialMapper.selectMaterialById(detail.getMaterialId());
                detail.setMaterial(material);
            }
        }

        return tblPurchaseOrder;
    }

    /**
     * 查询采购订单列表
     *
     * @param tblPurchaseOrder 采购订单
     * @return 采购订单
     */
    @Override
    public List<TblPurchaseOrder> selectTblPurchaseOrderList(TblPurchaseOrder tblPurchaseOrder)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();

        //获取物料
        List<TblPurchaseOrder> list=tblPurchaseOrderMapper.selectTblPurchaseOrderList(tblPurchaseOrder);
        for (TblPurchaseOrder purchaseOrder:list){
            int num=purchaseOrder.getTblPurchaseOrderDetailList().get(0).getPurchaseType();
            if (num==1){
                for (TblPurchaseOrderDetail detail:purchaseOrder.getTblPurchaseOrderDetailList()){
                    Material material=materialMapper.selectMaterialById(detail.getMaterialId());
                    detail.setMaterial(material);
                }
            }
        }

        return list;
    }

    /**
     * 新增采购订单
     *
     * @param tblPurchaseOrder 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblPurchaseOrder(TblPurchaseOrder tblPurchaseOrder)
    {
        tblPurchaseOrder.setCreateTime(DateUtils.getNowDate());

        tblPurchaseOrder.setPurchaserId(loginUser.getUser().getUserId().toString());
        tblPurchaseOrder.setCreateById(loginUser.getUser().getUserId().toString());
        tblPurchaseOrder.setCreateBy(loginUser.getUser().getUserName());

        int rows = tblPurchaseOrderMapper.insertTblPurchaseOrder(tblPurchaseOrder);
        insertTblPurchaseOrderDetail(tblPurchaseOrder);
        insertTblOrderPayType(tblPurchaseOrder);
        return rows;
    }

    /**
     * 修改采购订单
     *
     * @param tblPurchaseOrder 采购订单
     * @return 结果
     */
    @Transactional

    @Override
    public int updateTblPurchaseOrder(TblPurchaseOrder tblPurchaseOrder)
    {
        tblPurchaseOrder.setUpdateTime(DateUtils.getNowDate());
        tblPurchaseOrder.setUpdateById(loginUser.getUser().getUserId().toString());
        tblPurchaseOrder.setUpdateBy(loginUser.getUser().getUserName());
        if (tblPurchaseOrder.getApproveBy()!=null){
            tblPurchaseOrder.setApproveBy((loginUser.getUser().getUserName()));
        }

        //订单详情数据修改
        tblPurchaseOrderMapper.deleteTblPurchaseOrderDetailByOrderId(tblPurchaseOrder.getId());
        insertTblPurchaseOrderDetail(tblPurchaseOrder);

        //订单支付数据修改
        tblOrderPayTypeMapper.deleteTblOrderPayTypeById(tblPurchaseOrder.getId());
        insertTblOrderPayType(tblPurchaseOrder);

        return tblPurchaseOrderMapper.updateTblPurchaseOrder(tblPurchaseOrder);
    }

    /**
     * 批量删除采购订单
     *
     * @param ids 需要删除的采购订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblPurchaseOrderByIds(String[] ids)
    {
        tblPurchaseOrderMapper.deleteTblPurchaseOrderDetailByOrderIds(ids);
        return tblPurchaseOrderMapper.deleteTblPurchaseOrderByIds(ids);
    }

    /**
     * 删除采购订单信息
     *
     * @param id 采购订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblPurchaseOrderById(String id)
    {
        tblPurchaseOrderMapper.deleteTblPurchaseOrderDetailByOrderId(id);
        tblOrderPayTypeMapper.deleteTblOrderPayTypeById(id);
        return tblPurchaseOrderMapper.deleteTblPurchaseOrderById(id);
    }

    /**
     * 新增采购订单明细信息
     *
     * @param tblPurchaseOrder 采购订单对象
     */
    public void insertTblPurchaseOrderDetail(TblPurchaseOrder tblPurchaseOrder)
    {
        List<TblPurchaseOrderDetail> tblPurchaseOrderDetailList = tblPurchaseOrder.getTblPurchaseOrderDetailList();
        String id = tblPurchaseOrder.getId();
        if (StringUtils.isNotNull(tblPurchaseOrderDetailList))
        {
            List<TblPurchaseOrderDetail> list = new ArrayList<TblPurchaseOrderDetail>();
            for (TblPurchaseOrderDetail tblPurchaseOrderDetail : tblPurchaseOrderDetailList)
            {
                tblPurchaseOrderDetail.setOrderId(id);
                list.add(tblPurchaseOrderDetail);
            }
            if (list.size() > 0)
            {
                tblPurchaseOrderMapper.batchTblPurchaseOrderDetail(list);
            }
        }
    }

    /**
     * 新增采购支付记录
     *
     * @param tblPurchaseOrder 采购订单对象
     */
    public void insertTblOrderPayType(TblPurchaseOrder tblPurchaseOrder)
    {
        List<TblOrderPayType> tblOrderPayTypeList = tblPurchaseOrder.getTblOrderPayTypeList();
        String id = tblPurchaseOrder.getId();
        if (StringUtils.isNotNull(tblOrderPayTypeList))
        {
            List<TblOrderPayType> list = new ArrayList<TblOrderPayType>();
            for (TblOrderPayType tblOrderPayType : tblOrderPayTypeList)
            {
                tblOrderPayType.setOrderId(id);
                list.add(tblOrderPayType);
            }
            if (list.size() > 0)
            {
                for (TblOrderPayType tblOrderPayType : list) {
                    tblOrderPayTypeMapper.insertTblOrderPayType(tblOrderPayType);
                }
            }
        }
    }
}
